#include<iostream>
#include<string>

using namespace std;

#define InitSize 5 //定义表的初始长度

typedef struct {
	int* data;               //指示动态分配的指针
	int MaxSize, length;     //数组的最大容量和当前个数
}SeqList;                    //动态分配数组顺序表的类型定义

//初始化
bool InitList(SeqList &L) {
	L.data = (int *)malloc(sizeof(int) * InitSize);
	//L.data = new int[InitSize]{};
	//L.data = make_unique<int[]>(InitSize); //根据微软建议  C26409 改为使用智能指针工厂函数，如 std::make_unique 申请空间
	if (L.data == NULL)
		return false;
	L.MaxSize = InitSize;
	L.length = 0;
	return true;
}

//对数组进行扩容
bool ListBigger(SeqList& L, int i = 1) {

	int* p = (int*)malloc(sizeof(int) * (L.MaxSize + i));
	int j = 0;
	for (j = 0; j < L.length; j++) {
		p[j] = L.data[j];
	}
	free(L.data);
	L.data = p;
	L.MaxSize = L.MaxSize + i;
	cout << "当前长度" << L.MaxSize << endl;
	return true;
}

//插入指定位置的元素
bool ListInsert(SeqList& L, int i, int e) {
	if (i < 1 || i > L.length + 1) {
		return false;
	}
    if(L.length >= L.MaxSize){
		cout << "是否要拓展容量" << endl;
		int k;
		cin >> k;
		if (k == 0) {
			return false;
		}
		ListBigger(L);
	}
	int j = L.length;
	for (j = L.length; j >= i; j--) {
		L.data[j] = L.data[j - 1];
	}
	L.data[i - 1] = e;
	L.length++;
	return true;
}

//销毁方法
bool DestroyList(SeqList& L) {
	free(L.data);
	L.data == NULL;              //避免出现野指针
	L.length = 0;
	L.MaxSize = InitSize;
	return true;
}

//输出线性表元素
void PrintList(SeqList L) {
	int i = L.length;
	int j;
	for (j = 0; j < i; j++) {
		cout << L.data[j] << " ";
	}
	cout << endl;
}

//删除指定元素并返回
bool ListDelete(SeqList& L, int i, int &p) {
	if (i < 1 || i > L.length) {
		return false;
	}
	int j;
	p = L.data[i - 1];
	for (j = i - 1; j < L.length - 1; j++) {
		L.data[j] = L.data[j + 1];
	}
	L.length--;                                 //数据更改后在改变长度
	return true;
}


int main() {

	SeqList L;
	InitList(L);
	int i = 1;
	while (ListInsert(L, i, i)) {
		i++;
	}
	PrintList(L);
	ListInsert(L, 1, 4);
	PrintList(L);
	ListInsert(L, 1, 10);
	PrintList(L);
	int p;
	if (ListDelete(L, 5, p)) {
		cout << p << endl;
	}
	PrintList(L);

	cout << "你没事吧" << endl;
	/*DestroyList(L);*/
	/*
	cout << L.data[0] << endl;    //判断是否成功释放内存
	DestroyList(L);
	cout << L.data[0] << endl;
	*/
	return 0;
}